# class for response
#
module PaylerAPI
  class Response
    attr_reader :data

    def initialize httparty_response
      @data = httparty_response.parsed_response
    #rescue
      #@data = { 'error' => { 'code' => 0, 'message' => 'unknown' } }
    end

    def ok?
      data['order_id'] && !error?
    end
    alias_method :success?, :ok?

    def three_ds?
      data['auth_type'] == 1
    end

    def error?
      data.has_key? 'error'
    end

    def three_ds_attributes
      if three_ds?
        { url: data['acs_url'],
          pa_req: data['pareq'],
          md: data['md'] }
      else
        nil
      end
    end

    def error_code
      data['error']['code'] if error?
    end

    def duplicate_order_id?
      error? && error_code == 3
    end

    def error_message
      data['error']['message'] if error?
    end
    alias_method :message, :error_message

  end

end
